package cn.yhbk.web.modules.manager.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonPage;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.DTO.Student.StudentParamDTO;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbStudent;
import cn.yhbk.web.modules.manager.Pojo.VO.Student.StudentAdd;
import cn.yhbk.web.modules.manager.Pojo.VO.Student.StudentParam;
import cn.yhbk.web.modules.manager.Pojo.VO.Student.StudentUpdate;
import cn.yhbk.web.modules.manager.Service.IFlagService;
import cn.yhbk.web.modules.manager.Service.ITbStudentService;
import cn.yhbk.web.modules.ums.service.UmsRoleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.xml.transform.Result;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@RestController
@RequestMapping("/tbStudent")
@Slf4j
@Api(tags = "学生管理")
public class TbStudentController {

    @Autowired
    private ITbStudentService studentService;

    @Autowired
    private IFlagService flagService;

    @Autowired
    private UmsRoleService roleService;

    @ApiOperation("添加学生")
    @PostMapping("/manage/")
    public CommonResult addStudent(@RequestBody @Valid StudentAdd student){

        boolean success = studentService.add(student);
        if(success)return CommonResult.success("添加成功");
        return CommonResult.failed("添加失败!");
    }

    @ApiOperation("学生获取自己的信息")
    @GetMapping("/user/")
    public CommonResult getStudent(){
        Long adminId = MyUtil.getCurrentAdminId();
        TbStudent student = studentService.getStudentByAdminId(adminId);

        return CommonResult.success(student);
    }

    @ApiOperation("管理员获取学生信息")
    @GetMapping("/manage/")
    public CommonResult getStudents(@RequestParam(name = "currentPage",defaultValue = "1") Long currentPage,
                                    @RequestParam(name = "pageSize",defaultValue = "5") Long pageSize,
                                    StudentParam condition){
        //拿到辅导员角色的id
        Long studentRoleId = flagService.getStudentRoleId();
        List<Long> adminIds = roleService.getAdminIdsByRoleId(studentRoleId);
        log.debug("adminIds:{}",adminIds);
        if(adminIds.isEmpty())adminIds.add(-1L);

        StudentParamDTO studentParamDTO = new StudentParamDTO();
        BeanUtil.copyProperties(condition,studentParamDTO);
        studentParamDTO.setAdminIds(adminIds);

        Page<TbStudent> studentPage = studentService.getStudent(currentPage, pageSize, studentParamDTO);
        return CommonResult.success(CommonPage.restPage(studentPage));
    }

    @ApiOperation("修改学生信息")
    @PutMapping("/manage/{studentId}")
    public CommonResult updateStudentManage(@RequestBody StudentUpdate update,@PathVariable Long studentId){

        TbStudent student = new TbStudent();
        student.setId(studentId);
        BeanUtil.copyProperties(update,student);
        boolean success = studentService.updateById(student);
        if(success)return CommonResult.success("修改成功!");

        return CommonResult.failed("修改失败!");
    }

    @ApiOperation("修改学生信息")
    @PutMapping("/user/")
    public CommonResult updateStudent(@RequestBody StudentUpdate update){
        update.setStatus(null);

        //拿到当前用户得studentId
        Long adminId = MyUtil.getCurrentAdminId();
        TbStudent student = studentService.getStudentByAdminId(adminId);
        if(Objects.isNull(student))return CommonResult.failed("修改失败!请联系管理员");
        Long studentId = student.getId();

        TbStudent updateStudent = new TbStudent();
        updateStudent.setId(studentId);
        BeanUtil.copyProperties(update,updateStudent);

        boolean success = studentService.updateById(updateStudent);
        if(success)return CommonResult.success("修改成功!");

        return CommonResult.failed("修改失败!");
    }

    @ApiOperation("删除学生")
    @DeleteMapping("/manage/{studentId}")
    public CommonResult deleteStudent(@PathVariable Long studentId){
        boolean success = studentService.removeStudent(studentId);
        if(success)return CommonResult.success("删除成功!");

        return CommonResult.failed("删除失败!");
    }
}
